Ext.define("Mypocket.controller.Legislator", {
    extend: "Ext.app.Controller",
    requires: ["Mypocket.view.legislator.TabPanel", "Mypocket.view.bill.Show"],
    config: {
        control: {
            "#legislatorList": {
                itemtap: "onLegislatorTap" 
            },
            "#billList": {
                activate: "onBillListActivate",
                itemtap: "onBillTap"
            },
            "#voteList": {
                activate: "onVoteListActivate"
            },
            "#legislatorBackButton": {
                tap: function() {
                
                    Ext.getCmp("splashScreen").setActiveItem(Ext.getCmp("settingsForm"));
                    var b = Ext.getCmp("legislatorBio").getData(),
                    a = Ext.getStore("Legislators");             
                    var d = Ext.getStore("Bills"),
                    c = Ext.getStore("Votes");
                    a.remove(b);
                    c.removeAll();
                    d.removeAll()
                }
            },
            "#billSummaryBackButton": {
                tap: function() {                	
                	Ext.getCmp("splashScreen").setActiveItem(Ext.getCmp("legislatorTabPanel"));
                }
            }
          
        }
    },
    onLegislatorShow: function() {},
    onBillListActivate: function() {
        this.currentBills = this.currentLegislator;
        Ext.getCmp("billList").setData(this.currentBills.office)
    },
    onBillTap: function(b, a, d, c) {
        Ext.getStore("Bills");
        this.currentBill == c.data ? Ext.getCmp("viewport").setActiveItem(1) : (this.currentBill = c.data, (b = Ext.getCmp("billSummary")) ? (b.setData(c.data), Ext.getCmp("viewport").setActiveItem(Ext.getCmp("billSummary"))) : Ext.getCmp("viewport").setActiveItem({
            xclass: "Mypocket.view.bill.Show",
            data: c.data
        }), Ext.getCmp("billList").setData(c.data), Ext.getCmp("legislatorTabPanel").setActiveItem(0), Ext.getCmp("billSummaryToolbar").setTitle("\u91cd\u70b9\u79d1\u5ba4"))
    },
    onVoteListActivate: function() {
        var b = Ext.getStore("Votes"),
        a = Ext.getCmp("voteList");
        this.currentVotes = this.currentLegislator;
        a ? Ext.getCmp("voteList").setData(this.currentVotes.teachers) : (Ext.getCmp("voteList").setData(this.currentVotes.teachers), b.removeAll())
    },
    onLegislatorTap: function(b, a, d, c) {
    	
    	if(this.currentLegislator == c.data){
    		
    		 Ext.getCmp("splashScreen").setActiveItem(0);		
    		}
    	else {
    		
    		this.currentLegislator = c.data
    		if(Ext.getCmp("legislatorTabPanel")){    		
    			Ext.getCmp("legislatorBio").setData(c.data);
    			Ext.getCmp("splashScreen").setActiveItem(Ext.getCmp("legislatorTabPanel"));
    		}
    		else {
    			
    			Ext.getCmp("splashScreen").setActiveItem({
    	            xclass: "Mypocket.view.legislator.TabPanel"
    	        });
    	        Ext.getCmp("legislatorBio").setData(this.currentLegislator); 
    	        Ext.getCmp("legislatorToolbar").setTitle(this.currentLegislator.abbr);
    	        Ext.getCmp("legislatorTabPanel").setActiveItem(0);
    		}
    	}
//        this.currentLegislator == c.data ? Ext.getCmp("viewport").setActiveItem(0) 
//        		: (this.currentLegislator = c.data, Ext.getCmp("legislatorTabPanel") ? (Ext.getCmp("legislatorBio").setData(c.data), 
//        		Ext.getCmp("viewport").setActiveItem(1)) : 
//        			Ext.getCmp("viewport").setActiveItem({
//            xclass: "Mypocket.view.legislator.TabPanel"
//        }), 
//        Ext.getCmp("legislatorBio").setData(this.currentLegislator), 
//        Ext.getCmp("legislatorToolbar").setTitle(this.currentLegislator.abbr),
//        Ext.getCmp("legislatorTabPanel").setActiveItem(0))
    }
});